package com.stockz.ui.swing.charting.listener;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import com.stockz.core.util.Global;
import com.stockz.ui.swing.charting.util.UIChartingConstant;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.charting.view.StockChartView;

public class StockChartViewListener implements InternalFrameListener, FocusListener, MouseListener {

	private static void setSelectedStockChartPanel(StockChartPanel stockChartPanel){
		Global.put(UIChartingConstant.SelectedStockChartPanel, stockChartPanel);
	}
	
	private static final StockChartViewListener instance = new StockChartViewListener();
	public static StockChartViewListener getInstance(){
		return instance;
	}
	private StockChartViewListener() { }

	
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource() instanceof StockChartView){
			setSelectedStockChartPanel(((StockChartView)e.getSource()).getStockChartPanel());
		}
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		if(e.getSource() instanceof StockChartView){
			setSelectedStockChartPanel(((StockChartView)e.getSource()).getStockChartPanel());
		}
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}@Override
	public void mousePressed(MouseEvent e) {
		if(e.getSource() instanceof StockChartView){
			setSelectedStockChartPanel(((StockChartView)e.getSource()).getStockChartPanel());
		}
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void focusGained(FocusEvent e) {
		if(e.getSource() instanceof StockChartView){
			Global.put(UIChartingConstant.SelectedStockChartPanel, ((StockChartView)e.getSource()).getStockChartPanel());
		}
	}
	
	@Override
	public void focusLost(FocusEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void internalFrameOpened(InternalFrameEvent e) {
		setSelectedStockChartPanel(((StockChartView)e.getInternalFrame()).getStockChartPanel());
	}

	@Override
	public void internalFrameClosing(InternalFrameEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void internalFrameClosed(InternalFrameEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void internalFrameIconified(InternalFrameEvent e) {
		e.getInternalFrame().moveToFront();
	}

	@Override
	public void internalFrameDeiconified(InternalFrameEvent e) {
		setSelectedStockChartPanel(((StockChartView)e.getInternalFrame()).getStockChartPanel());
	}

	@Override
	public void internalFrameActivated(InternalFrameEvent e) {
		setSelectedStockChartPanel(((StockChartView)e.getInternalFrame()).getStockChartPanel());
	}

	@Override
	public void internalFrameDeactivated(InternalFrameEvent e) {
		// TODO Auto-generated method stub

	}

}
